package com.qf.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

import java.util.Scanner;
/**
 * projectName: 2103--RBAC
 *
 * @author: MaTianWei
 * time: 2021/7/9 10:18
 * description:
 */

public class AuthenticationDemo {
    public static void main(String[] args) {

        //1·创建securityManager的工程
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        //2·制造securityManager
        SecurityManager securityManager = factory.getInstance();
        //3·将securityManager放入全局变量 辅助工具
        SecurityUtils.setSecurityManager(securityManager);
        //4·获取subject
        Subject subject = SecurityUtils.getSubject();
        //5·认证 或者 授权
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入账号");
        String account = scanner.nextLine();
        System.out.println("输入密码");
        String password = scanner.nextLine();

        UsernamePasswordToken token = new UsernamePasswordToken(account,password);

        try {
            subject.login(token);
            System.out.println("认证成功");
        }catch(AuthenticationException e){
            System.out.println("认证失败");
        }
        //6·取消认证 退出登录
        subject.logout();
    }
}
